#include "stdafx.h"
#include "SysManager.h"
#include "SysUnit.h"
#include "ChartMultiData.h"
#include "JamesSys.h"

CSysManager::CSysManager()
{
	m_RunPast = false;
}


CSysManager::~CSysManager()
{
	for (SystemListPos pos = m_SysList.begin(); pos != m_SysList.end(); pos++)
	{
		CSysUnit* curSys = *pos;
		delete curSys;
		curSys = NULL;
	}
}

CSysUnit* CSysManager::AddSystem(CSysUnit* a_Sys)
{
	if (!a_Sys)
		return NULL;
	m_SysList.push_back(a_Sys);

	return a_Sys;
}

void CSysManager::Run(int a_Index)
{
	if (a_Index < 0)
		return;
	for (SystemListPos pos = m_SysList.begin(); pos != m_SysList.end(); pos++)
	{
		CSysUnit* curSys = *pos;
		curSys->Run(a_Index);
	}
}

void CSysManager::PreRun(CChartMultiData* a_MultiData)
{
	if (!a_MultiData || m_RunPast == true)
		return;
	for (SystemListPos pos = m_SysList.begin(); pos != m_SysList.end(); pos++)
	{
		CSysUnit* curSys = *pos;
		curSys->RealTime = false;
		curSys->MainData(a_MultiData);
	}

	for (int i = a_MultiData->S + ExtraDataCount; i < a_MultiData->E; i++)
	{
		Run(i);
	}

	m_RunPast = true;
}

CJamesSys* CSysManager::AddJamesSystem()
{
	CJamesSys* js = new CJamesSys(_T("JS"));
	AddSystem(js);
	return js;
}

void CSysManager::RealTimeRun()
{
	if (m_RunPast == false)
		return;

	for (auto pos = m_SysList.begin(); pos != m_SysList.end(); pos++)
	{
		CSysUnit* curSys = *pos;
		curSys->RealTime = true;
		curSys->Run();
	}
}
